package com.example.zhinongbao.view;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zhinongbao.R;
import com.example.zhinongbao.adapter.GoodsListAdapter;
import com.example.zhinongbao.contract.GoodsSearchContract;
import com.example.zhinongbao.contract.MainContract;
import com.example.zhinongbao.entity.Goods;
import com.example.zhinongbao.presenter.GoodsSearchPresenter;
import com.example.zhinongbao.presenter.MainPresenter;

import java.util.ArrayList;
import java.util.List;

public class GoodsSearchActivity extends AppCompatActivity implements GoodsSearchContract.IGoodsSearchContractView {

    private EditText etSearchKey;
    private TextView tvSearch;
    private RecyclerView rvMain;
    private GoodsSearchPresenter goodsSearchPresenter;
    private GoodsListAdapter mRecyclerAdapter;
    private List<Goods> mGoodsList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_search);
        goodsSearchPresenter = new GoodsSearchPresenter(this, this);
        initView();
    }

    private void initView() {
        etSearchKey = findViewById(R.id.etSearchKey);
        tvSearch = findViewById(R.id.tvSearch);
        tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String key = etSearchKey.getText().toString();
                if (TextUtils.isEmpty(key)) {
                    Toast.makeText(GoodsSearchActivity.this, "请输入搜索内容", Toast.LENGTH_SHORT).show();
                    return;
                }
                search(key);
            }
        });
        rvMain = findViewById(R.id.rvMain);
        rvMain.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerAdapter = new GoodsListAdapter(mGoodsList);
        rvMain.setAdapter(mRecyclerAdapter);
    }

    private void search(String key) {
        goodsSearchPresenter.getGoodsList(key);
    }

    @Override
    public void showGoodsList(List<Goods> goodsList) {
        mRecyclerAdapter.setData(goodsList);
    }
}
